package event

import ext.byteArg

class DeliveryResultEvent(val row: Int, val col: Int, val state: Int) {
    companion object {
        fun withByteArray(byteArray: ByteArray): DeliveryResultEvent {
            val row = byteArray.byteArg(1)
            val col = byteArray.byteArg(2)
            val state = byteArray.byteArg(3)

            return DeliveryResultEvent(row, col, state)
        }
    }

    fun isSuccess() = state == 0x00

    fun isNext() = (state == 0x0B) || (state == 0x01) || (state == 0x0A) || (state == 0x08)

    fun isError() = (state >= 0x02) && (state < 0x08)

    fun result(): String {
        when (state) {
            0x00 -> return "出货成功"
            0x01 -> return "此货道无货"
            0x03 -> return "电机堵转"
            0x02 -> return "电机超时"
            0x04 -> return "无此货道"
            0x05 -> return "出货失败"
            0x06 -> return "光栅被遮挡,无法出货"
            0x07 -> return "电机其他故障"
            0x08 -> return "出货失败但客户没有及时取走货物"
            0x09 -> return "第二/三次开门成功"
            0x0A -> return "第二/三次关门但客户未及时取走货物"
            0x0B -> return "第一/二/三次关门并且客户取走货物"
            else -> return "未知异常"
        }
    }
}